<html>
<head>
  <title>Python keywords, identifiers and variables.</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="639"/>
<h1>Python keywords, identifiers and variables.</h1>

<div>
<span><div><div><img src="Python keywords, identifiers and variables._files/Image.png" type="image/png" data-filename="Image.png" width="607"/></div><div><br/></div><div><br/></div><h2 style="text-align: center;"><span style="color: rgb(28, 51, 135); font-size: 24pt;">Python Identifiers</span></h2><div><br/></div><div>        Python Identifiers are user-defined names to represent a variable, function, class, module or any other object. If you assign some name to a programmable entity in Python, then it is nothing but technically called an identifier.</div><div><font color="#AD0000" style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">a set of rules for programmers to create meaningful identifiers</span></font></div><div><span style="font-weight: bold;">1.</span> To form an identifier, use a <span style="font-weight: bold;">sequence of letters</span> either in <span style="font-weight: bold;">lowercase</span> <span style="font-weight: bold;">(a to z)</span> or <span style="font-weight: bold;">uppercase</span> <span style="font-weight: bold;">(A to Z)</span>. However, you can also mix up <span style="font-weight: bold;">digits (0 to 9)</span> or an <span style="font-weight: bold;">underscore (_)</span> while writing an identifier.</div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="font-weight: bold;">For example –</span> Names like <span style="font-weight: bold;">shapeClass</span>, <span style="font-weight: bold;">shape_1</span>, and <span style="font-weight: bold;">upload_shape_to_db</span> are all valid identifiers.</div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="font-weight: bold;">2.</span> You can’t use digits to begin an identifier name. It’ll lead to the syntax error.</div><div><span style="font-weight: bold;">For example –</span> The name, <span style="font-weight: bold;">0Shape</span> is incorrect, but <span style="font-weight: bold;">shape1</span> is a valid identifier.</div><div><span style="font-weight: bold;">3.</span> Also, the Keywords are reserved, so you should not use them as identifiers.</div><div><span style="font-weight: bold;">4.</span> Python Identifiers can also not have special characters <span style="font-weight: bold;">[‘.’, ‘!’, ‘@’, ‘#’, ‘$’, ‘%’]</span> in their formation. These symbols are forbidden.</div><div><span style="font-weight: bold;">5.</span> Python doc says that you can <span style="font-weight: bold;">have an identifier with unlimited length</span>. But it is just the half truth.</div><div>Using a large name (more than 79 chars) would lead to the violation of a rule set by the <span style="font-weight: bold;">PEP-8</span> standard. It says.</div><h3><span style="color: rgb(173, 0, 0); font-size: 12pt;">Testing If an Identifier is Valid.</span></h3><div>You can test whether a Python identifier is valid or not by using the <span style="font-weight: bold;">keyword.iskeyword()</span> function. It returns <span style="font-weight: bold;">“True”</span> if the keyword is correct or <span style="font-weight: bold;">“False”</span> otherwise.</div><div><br/></div><h2 style="text-align: center;"><span style="color: rgb(28, 51, 135); font-size: 24pt;">Python Variables.</span></h2><div><br/></div><div>        A variable in Python represents an entity whose value can change as and when required. Conceptually, it is a memory location which holds the actual value. And we can retrieve the value from our code by querying the entity.</div><div><br/></div><div><span style="font-weight: bold;">1.</span> Variables don’t require declaration. However, you must initialize them before use.</div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="font-weight: bold;">For example –</span> </div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">test = 10</span></div><div><br/></div></div><div><br/></div><div><br/></div><div><span style="font-weight: bold;">2.</span> The above expression will lead to the following actions.</div><ul><li><font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">Creation</span></font> of an object to represent the <font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">value 10</span></font>.</li><li>If the <font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">variable (test)</span></font> doesn’t exist, then it’ll <font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">get created</span></font>.</li><li><font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">Association of the variable with the object</span></font>, so that it can refer the value.</li></ul><div><img src="Python keywords, identifiers and variables._files/Image [1].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><span style="font-weight: bold;">3.</span> Whenever the expression changes, Python associates a new object (a chunk of memory) to the variable for referencing that value. And the <font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">old one goes to the garbage collector</span></font>.</div><div><span style="line-height: 1.45;">Example.</span></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">&gt;&gt;&gt; test = 10</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">&gt;&gt;&gt; id(test)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">1716585200</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">&gt;&gt;&gt; test = 11</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">&gt;&gt;&gt; id(test)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">1716585232</span></div><div><br/></div></div><div><br/></div><div><span style="font-weight: bold;">4.</span> Also, for optimization, Python builds a cache and <font style="font-size: 12pt;"><span style="color: rgb(173, 0, 0); font-size: 12pt; font-weight: bold;">reuses some of the immutable objects</span></font>, such as small integers and strings.</div><div><br/></div><div><span style="font-weight: bold;">5. </span>An object is just a region of memory which can hold the following.</div><ul><li>The actual object values.</li><li>A type designator to reflect the object type.</li><li>The reference counter which determines when it’s OK to reclaim the object.</li></ul><div><br/></div><div><span style="font-weight: bold;">6.</span> It’s the object which has a type, not the variable. However, a variable can hold objects of different types as and when required.</div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div></span>
</div></body></html> 